package com.ple.app.suggest;

import android.app.AlertDialog;
import android.app.LoaderManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SlidingDrawer;
import android.widget.Toast;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapOptions;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.runtime.ArcGISRuntime;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.ple.app.importdata.Image_opreation;
import com.ple.app.importdata.InputSeed_Activity;
import com.ple.app.importdata.MyopenHelper;
import com.ple.app.jzsf.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Display_decisionActivity extends Activity {

    private MapView mapView;
    private SlidingDrawer slidingDrawer;
    private ImageButton gloable_button;
    private final int THEMMAP=0;//专题图
    private final int GLOBAL=1;//影像
    private int TYPE=GLOBAL;
    private ImageButton thematic_button;
    private ImageView thematic_map;
    private Button hangdle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_decision);
        slidingDrawer= (SlidingDrawer) findViewById(R.id.slid);
        ArcGISRuntime.setClientId("9yNxBahuPiGPbsdi");//去水印
        mapView= (MapView) findViewById(R.id.suggest_map);
        final GraphicsLayer graphicsLayer=new GraphicsLayer();
        final GraphicsLayer graphicsLayer1=new GraphicsLayer();//显示状态
        mapView.addLayer(graphicsLayer);
        mapView.addLayer(graphicsLayer1);
        mapView.setOnStatusChangedListener(new OnStatusChangedListener() {
            @Override
            public void onStatusChanged(Object o, STATUS status) {
                if (status == STATUS.INITIALIZED) {
                   inidata(graphicsLayer);
                }
            }
        });
        mapView.setOnSingleTapListener(new OnSingleTapListener() {
            @Override
            public void onSingleTap(float v, float v1) {

                int[] m = graphicsLayer.getGraphicIDs(v, v1, 1);
                if(m.length>0){
                    slidingDrawer.open();
                    Graphic gp=graphicsLayer.getGraphic(m[0]);
                    Polygon py= (Polygon) gp.getGeometry();
                    Point p=(Point) GeometryEngine.project( py.getPoint(0),mapView.getSpatialReference(), SpatialReference.create(4326));
                    graphicsLayer1.removeAll();
                    SimpleFillSymbol simpleFillSymbol=new SimpleFillSymbol(Color.RED);
                    SimpleLineSymbol l=new SimpleLineSymbol(Color.RED,4);
                    simpleFillSymbol.setAlpha(0);
                    simpleFillSymbol.setOutline(l);
                    Graphic gpplay=new Graphic(py,simpleFillSymbol);
                    graphicsLayer1.addGraphic(gpplay);
                }
                else{
                    graphicsLayer1.removeAll();
                }
            }
        });
        /**
         * 影像与专题图切换
         */
        gloable_button= (ImageButton) findViewById(R.id.globle_button);
        thematic_button= (ImageButton) findViewById(R.id.thematic_button);
        thematic_map= (ImageView) findViewById(R.id.thematic_map);
        Image_opreation image_opreation=new Image_opreation(thematic_map);
        thematic_map.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new AlertDialog.Builder(Display_decisionActivity.this).setTitle("系统提示！")
                        .setMessage("确认保存此图片？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                               saveImage(thematic_map);
                                Toast.makeText(getApplicationContext(),"已保存",Toast.LENGTH_LONG).show();


                            }
                        }).setNegativeButton("取消",null).create().show();
                return false;
            }
        });




        gloable_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TYPE==GLOBAL){return;}
                else{
                    TYPE=GLOBAL;
                    thematic_map.setVisibility(View.GONE);
                    mapView.setVisibility(View.VISIBLE);
                    slidingDrawer.setVisibility(View.VISIBLE);
                }
            }
        });
        thematic_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TYPE==THEMMAP){return;}
                else{
                    TYPE=THEMMAP;
                    thematic_map.setVisibility(View.VISIBLE);
                    mapView.setVisibility(View.GONE);
                    slidingDrawer.setVisibility(View.GONE);
                }
            }
        });
        /**
         * 抽屉效果
         */



    }//oncreat
private void inidata(final GraphicsLayer graphicsLayer){
    Point p=new Point();
    Point po;
    p.setX(116.1904);
    p.setY(40.2148);
    /**
     * 创建多边形
     */
    Polygon py1;
    Polygon poly1=new Polygon();
    poly1.startPath(116.1885,40.2182);
    poly1.lineTo(116.1883 ,40.2162);
    poly1.lineTo(116.1919, 40.2159);
    poly1.lineTo(116.1916 ,40.2180);
    poly1.lineTo(116.1885,40.2182);

    Polygon py2;
    Polygon poly2=new Polygon();
    poly2.startPath(116.1919, 40.2159);
    poly2.lineTo(116.1916, 40.2180);
    poly2.lineTo(116.1938 ,40.2179);
    poly2.lineTo(116.1934 ,40.2155);
    poly2.lineTo(116.1919, 40.2159);

    Polygon py3;
    Polygon poly3=new Polygon();
    poly3.startPath(116.1883 ,40.2162);
    poly3.lineTo( 116.1919 ,40.2159);
    poly3.lineTo(116.1934, 40.2155);
    poly3.lineTo(116.1915 ,40.2112);
    poly3.lineTo(116.1876 ,40.2119);
    poly3.lineTo(116.1883 ,40.2162);

    SimpleFillSymbol simpleFillSymbol=new SimpleFillSymbol(Color.RED);
    simpleFillSymbol.setAlpha(50);
    SimpleFillSymbol simpleFillSymbol2=new SimpleFillSymbol(Color.BLUE);
    simpleFillSymbol2.setAlpha(50);
    SimpleFillSymbol simpleFillSymbol3=new SimpleFillSymbol(Color.GREEN);
    simpleFillSymbol3.setAlpha(50);
    py1= (Polygon) GeometryEngine.project(poly1, SpatialReference.create(4326),mapView.getSpatialReference());
    Graphic gp1=new Graphic(py1,simpleFillSymbol);
    graphicsLayer.addGraphic(gp1);
    py2= (Polygon) GeometryEngine.project(poly2, SpatialReference.create(4326),mapView.getSpatialReference());
    Graphic gp2=new Graphic(py2,simpleFillSymbol2);
    graphicsLayer.addGraphic(gp2);
    py3= (Polygon) GeometryEngine.project(poly3, SpatialReference.create(4326),mapView.getSpatialReference());
    Graphic gp3=new Graphic(py3,simpleFillSymbol3);
    graphicsLayer.addGraphic(gp3);

    po=(Point) GeometryEngine.project(p, SpatialReference.create(4326),mapView.getSpatialReference());//经纬度转投影
   // mapView.zoomTo(po,8);
    mapView.setExtent(py1);
}
    /**
     * 保存图片到本地
     */
    private void saveImage(ImageView imageView){
        imageView.buildDrawingCache();
        Bitmap bitmap=imageView.getDrawingCache();
        File appdir=new File(Environment.getExternalStorageDirectory()+"/saveimg");
        if(!appdir.exists())appdir.mkdir();
        File sdcard=new File(appdir,"/yangzhong.jpg");
        try {
            FileOutputStream fileOutputStream=new FileOutputStream(sdcard);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();

            /**
             * 通知广播更新
             */
            Intent intent=new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri= Uri.fromFile(sdcard);
            intent.setData(uri);
            Display_decisionActivity.this.sendBroadcast(intent);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }//saveImage(ImageView imageView)
}
